package io.gitee.kenewstar.dbmq.handler;

import io.gitee.kenewstar.dbmq.ConsumerTask;
import io.gitee.kenewstar.dbmq.DbMqConfigProperties;
import io.gitee.kenewstar.dbmq.DbMqConsumerInf;
import io.gitee.kenewstar.dbmq.data.DbDataHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kenewstar
 */
public class DbMqAssembleHandler extends DbMqCoreHandler {

    private DbMqConsumerTaskHandler dbMqConsumerTaskHandler;
    private PullMessageHandler pullMessageHandler;
    private List<DbMqConsumerInf> infList;
    private List<ConsumerTask> taskList;

    public DbMqAssembleHandler() {}

    public DbMqAssembleHandler(List<DbMqConsumerInf> infList,
                               DbDataHandler dbDataHandler,
                               DbMqConfigProperties dbMqConfigProperties,
                               DbMqConsumerTaskHandler dbMqConsumerTaskHandler,
                               PullMessageHandler pullMessageHandler) {
        this.infList = infList;
        this.dbDataHandler = dbDataHandler;
        this.dbMqConfigProperties = dbMqConfigProperties;
        this.dbMqConsumerTaskHandler = dbMqConsumerTaskHandler;
        this.pullMessageHandler = pullMessageHandler;
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        this.taskList = new ArrayList<>(infList.size());
        checkConsumerDuplicate(infList, taskList);
        initRegisterInfo();
        initQueueInfo(taskList);
        registerConsumer(taskList);
        this.dbMqConsumerTaskHandler.init(taskList);
        this.pullMessageHandler.setTaskList(taskList);
        this.dbMqConsumerTaskHandler.startup();
    }
}
